package com.octopus.kernal.ui.element;

import java.lang.reflect.InvocationTargetException;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.beanutils.BeanUtils;

import com.octopus.kernal.ui.builder.DetailTableBuilder;

@SuppressWarnings("serial")
@XmlType(name="TextareaElement")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class TextareaElement extends BaseElement{
	
	@XmlAttribute
	private String width;
	
	@XmlAttribute
	private String height;

	@Override
	public String toString(){
		StringBuffer str = new StringBuffer();
		StringBuffer strhidden = new StringBuffer();
		if(this.isReadonly()){
			if(this.getValue() != null)
				str.append(getTranslationValue(getValue().toString()));
			return str.toString().replaceAll(DetailTableBuilder.sp, "<br>");
		}else{
			str.append("<textarea  ");
			strhidden.append("<input type=\"hidden\" ");
			if(this.getId() != null){
				str.append("id=\"" + this.getId() + "\" ");
				strhidden.append("id=\"" + this.getId() + "\" ");
			}
			if(this.getName() != null){
				str.append("name=\"" + this.getName() + "\" ");
				strhidden.append("name=\"" + this.getName() + "\" ");
			}
			if(this.getOnfocus() != null)
				str.append("onfocus=\"" + this.getOnfocus().toString() + "\" ");
			if(this.getCssclass() != null)
				str.append("class=\"" + this.getCssclass().toString() + "\" ");
			if(this.isDisabled())
				str.append("disabled=\"disabled\" ");
			if(this.getStyle() != null)
				str.append("style=\"" + this.getStyle().toString() + "\" ");
			str.append(">");
			if(this.getValue() != null){
				str.append(this.getValue().toString());
				strhidden.append("value=\"" + this.getValue().toString() + "\" ");
			}
			str.append("</textarea>");
			strhidden.append(">");
			if(this.isDisabled() && this.getValue() != null)
				str.append(strhidden.toString());
			return str.toString();
		}
	}
	
	public TextareaElement clone(){
		TextareaElement result = new TextareaElement();
		try {
			BeanUtils.copyProperties(result, this);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getStyle() {
		return "margin:2px;" + (null==width?"":("width:" + width + ";")) + (null==height?"":("height:" + height + ";"));
	}

}
